package com.fg.cloud.agency.controller.order;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fg.cloud.agency.controller.BaseAgencyController;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.OrderStatus;
import com.fg.cloud.common.finals.UserType;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.reflection.ParamsReflect;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.order.OrderReturnEntity;
import com.fg.cloud.manager.entry.order.vo.OrderReturnDetailVo;
import com.fg.cloud.manager.entry.order.vo.OrderReturnVo;
import com.fg.cloud.manager.service.factory.FactoryService;
import com.fg.cloud.manager.service.order.OrderRefundsService;

/**
 * 
 * Description:退货单
 * @author 陈银龙
 * @date 2017年8月29日上午10:42:40
 */
@Controller
@RequestMapping("agencyOrderRefund")
public class AgencyOrderRefundsController extends BaseAgencyController{

	@Autowired
	private OrderRefundsService orderRefundsService;
	@Autowired
	private FactoryService factoryService;
	
	
	/**
	 * Description:基础查询,关键词（发货单号、订单号、经销商名称、物流公司名称,快递单号）；
	 * @param request
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param keyword
	 * @author 陈银龙
	 * @date 2017年8月29日上午10:50:20
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("findList")
	@ResponseBody
	public void findList(HttpServletRequest request, HttpServletResponse response, 
			Integer page, Integer rows, String sort, String order, OrderReturnVo entity){
		try {
			PageUtils<OrderReturnEntity> pageinfo = 
					new PageUtils<OrderReturnEntity>(page, rows, "sr."+sort, order);
			AgencyUserVo user = getCurrentAgencyUser();
			entity.setAgeId(user.getAgeId());
			entity.setFacId(user.getFacId());
			
			Map<String, Object> condition = ParamsReflect.getParamsByReflect(entity);

			pageinfo.setCondition(condition);
			orderRefundsService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	/**
	 * Description:退货单详情
	 * @param request
	 * @param response
	 * @param pageNum
	 * @param pageSize
	 * @param keyword
	 * @author 陈银龙
	 * @date 2017年8月29日上午10:50:20
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("findDetail")
	@ResponseBody
	public void findDetail(HttpServletRequest request, HttpServletResponse response, Long id){
		try {
			if(id == null){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			OrderReturnEntity oe = orderRefundsService.find(id, user.getFacId(), user.getAgeId());
			
			doResult(response, true, StatusCode.FINDSUCCESS, oe, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}
	
	
	/**
	 * Description:退货单新增
	 * @param request
	 * @param response
	 * @param dates
	 * @author 陈银龙
	 * @date 2017年8月29日上午11:44:51
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("insert")
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response,String data){
		try {
			if(data == null){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			OrderReturnVo entity = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(data), OrderReturnVo.class);
			if( entity.getExpressCompany() == null
					|| entity.getExpressCompanyNo() == null || entity.getAgeName() == null){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			entity.setAgeId(user.getAgeId());
			entity.setFacId(user.getFacId());
			entity.setStatus(OrderStatus.RETURN_SAVE);
			entity.setReturnDate(new Date());
			setCreateInfo(entity, request);
			setUpdateInfo(entity, request);
			
			boolean flag = orderRefundsService.insert(entity);
			
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, 
					null, flag ? ResultType.ADD_SUCCESS:ResultType.ADD_FAULT);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.ADD_FAULT);
		}
	}
	
	/**
	 * 
	 * Description:退货单修改
	 * @param request
	 * @param response
	 * @param dates
	 * @author 陈银龙
	 * @date 2017年8月29日上午11:44:51
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("update")
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response,String data){
		try {
			OrderReturnVo entity = JSON.parseObject(
					StringEscapeUtils.unescapeHtml4(data), OrderReturnVo.class);
			if(entity.getId() == null || entity.getId() < 0 ){
				doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			entity.setAgeId(user.getAgeId());
			entity.setFacId(user.getFacId());
			setUpdateInfo(entity, request);
			boolean flag = orderRefundsService.update(entity);
			doResult(response, flag, flag ? StatusCode.SAVESUCCESS:StatusCode.SAVEERROR, 
					null, flag ? ResultType.SAVE_SUCCESS:ResultType.SAVE_FAULT);
			
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.SAVE_FAULT);
		}
	}
	
	/**
	 * 
	 * Description:退货单删除
	 * @param request
	 * @param response
	 * @param dates
	 * @author 陈银龙
	 * @date 2017年8月29日上午11:44:51
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("deletes")
	@ResponseBody
	public void deletes(HttpServletRequest request, HttpServletResponse response,
			String ids){
		try {
			if(null == ids || 0 == ids.length()){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			boolean flag = orderRefundsService.deletes(LongUtils.convert(ids.split("[, ]+")),
					user.getFacId(), user.getAgeId());
			doResult(response, flag, flag ? StatusCode.REMOVESUCCESS:StatusCode.REMOVEERROR, 
					null, flag ? ResultType.REMOVE_SUCCESS:"未查到数据或者已进入流程");
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.REMOVE_FAULT);
		}
	}
	
	/**
	 * Description:提交
	 * @param request
	 * @param response
	 * @param entity
	 * @author around
	 * @date 2017年9月6日下午5:36:51
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("confirm")
	@ResponseBody
	public void confirm(HttpServletRequest request, HttpServletResponse response, 
			OrderReturnEntity entity){
		try {
			if(null == entity.getId()){
				doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			entity.setAgeId(user.getAgeId());
			entity.setFacId(user.getFacId());
			setUpdateInfo(entity, request);
			boolean flag = orderRefundsService.confirm(entity);
			doResult(response, flag, flag ? StatusCode.OPERATIONSUCCESS:StatusCode.OPERATIONERROR, 
					null, flag ? ResultType.OPERATION_SUCCESS:ResultType.OPERATION_FAULT);

		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	/**
	 * Description:发货
	 * @param request
	 * @param response
	 * @param entity
	 * @author around
	 * @date 2017年9月6日下午11:57:10
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("delive")
	@ResponseBody
	public void delive(HttpServletRequest request, HttpServletResponse response, 
			OrderReturnEntity entity){
		try {
			if(null == entity.getId()){
				doResult(response, false, StatusCode.LACKVALUE, null, "id不能为空");
				return;
			}
			AgencyUserVo user = getCurrentAgencyUser();
			entity.setAgeId(user.getAgeId());
			entity.setFacId(user.getFacId());
			setUpdateInfo(entity, request);
			entity.setSendDate(new Date());
			boolean flag = orderRefundsService.delive(entity);
			doResult(response, flag, flag ? StatusCode.OPERATIONSUCCESS:StatusCode.OPERATIONERROR, 
					null, flag ? ResultType.OPERATION_SUCCESS:ResultType.OPERATION_FAULT);

		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	/**
	 * Description:订单详情转退货单详情新增
	 * @param response
	 * @param ids
	 * @author around
	 * @date 2017年9月6日下午11:56:41
	 */
	@RequiresRoles(UserType.AGENCY)
	@RequestMapping("selectReturnDetail")
	@ResponseBody
	public void selectReturnDetail(HttpServletResponse response, String ids){
		try {
			if(null == ids || 0 == ids.length()){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			List<OrderReturnDetailVo> list = orderRefundsService.findByOrderId(
					LongUtils.convert(ids.split("[, ]+")));
			doResult(response, true, StatusCode.OPERATIONSUCCESS, 
					list, ResultType.OPERATION_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	/**
	  * Description:显示厂商信息
	  * @param request
	  * @param response
	  * @author shaoqiping
	  * @date 2017年9月7日上午11:56:35
	*/
	@RequiresRoles(value={UserType.AGENCY,UserType.GUEST},logical=Logical.OR)
	@RequestMapping("facInfo")
	@ResponseBody
	public void facInfo(HttpServletRequest request, HttpServletResponse response){
		try {
			doResult(response, true, StatusCode.FINDSUCCESS, factoryService.find(getCurrentAgencyUser().getFacId()), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.OPERATION_FAULT);
		}
	}
	
	private String setSort(String sort) {
		if ("updateTime".indexOf(sort) != -1)
			return "sr.updateTime";
		return sort;
	}
	
	
	/*public void findBySenior(HttpServletRequest request, HttpServletResponse response,OrderReturnVo ov){
		try {
			if(ov.getPageNum() == null || ov.getPageNum() < 0 || ov.getPageSize() == 0 || ov.getPageSize() < 0){
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			PageUtils<OrderReturnEntity> pageinfo = new PageUtils<OrderReturnEntity>(ov.getPageNum(), ov.getPageSize());
			AgencyUserVo user = getCurrentAgencyUser();
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("facId", user.getFacId());
			condition.put("returnNo", ov.getReturnNo());
			condition.put("orderNo", ov.getOrderNo());
			condition.put("beginDate", ov.getBeginDate());
			condition.put("endDate", ov.getEndDate());
			condition.put("ageId", user.getAgeId());
			condition.put("ageName", ov.getAgeName());
			condition.put("ageLevelId", ov.getAgeLevelId());
			condition.put("areaId", ov.getAreaId());
			condition.put("areaNumber", ov.getAreaNumber());
			condition.put("productInfo", ov.getProductInfo());
			condition.put("receiveInfo", ov.getReceiveInfo());
			condition.put("status", ov.getStatus());
			orderRefundsService.findBySenior(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, ResultType.FIND_FAULT);
		}
	}*/

}
